package com.dylan.历史.基础;

import java.time.Duration;
import java.time.Instant;

/**
 * Created by Dylan on 2018/8/27.
 */
public class 字符串 {

    public static void main(String[] args) {
        String str1 = "ab";
        String str2 = "ab";
        String str3 = "a".concat("b");
        System.out.println(str1 == str2); // true
        System.out.println(str1 == str3); // false

        String name = "安康";
        System.out.println(new String(name.getBytes()));

        System.out.println(diff1()); // 6760, 使用+号，效率最低
        System.out.println(diff2()); // 1248
        System.out.println(diff3()); // 2，效率最高
    }

    static long diff1() {
        Instant start = Instant.now();
        String str = "a";
        for (int i=0;i<100000;i++) {
            str = str + "b";
        }
        Instant end = Instant.now();
        return Duration.between(start, end).toMillis();
    }

    static long diff2() {
        Instant start = Instant.now();
        String str = "a";
        for (int i=0;i<100000;i++) {
            str = str.concat("b");
        }
        Instant end = Instant.now();
        return Duration.between(start, end).toMillis();
    }

    static long diff3() {
        Instant start = Instant.now();
        StringBuilder str = new StringBuilder("a");
        for (int i=0;i<100000;i++) {
            str.append("b");
        }
        Instant end = Instant.now();
        return Duration.between(start, end).toMillis();
    }
}
